package com.qgzj.controller.admin.ajax;

import com.qgzj.common.vo.*;
import com.qgzj.controller.BaseController;
import com.qgzj.entity.*;
import com.qgzj.exception.QgzjException;
import com.qgzj.exception.QgzjStudyExceptionEnum;
import com.qgzj.mapper.ContentMapper;
import com.qgzj.response.CommonRes;
import com.qgzj.response.PageQueryRes;
import com.qgzj.service.*;
import com.qgzj.util.DUIOkHttpClient;
import com.qgzj.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @ClassName ApiController
 * @DATE 2019-11-12 22:27
 **/
@RestController
@RequestMapping("/admin/ajax/study")
@Slf4j
public class AdminAjaxController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(AdminAjaxController.class);

    @Autowired
    TeachingMaterialService materialService;

    @Autowired
    VolumeService volumeService;

    @Autowired
    CatalogService catalogService;

    @Autowired
    ContentService contentService;

    @Autowired
    ContentDetailService contentDetailService;

    @Resource
    ContentMapper contentMapper;

    private static final String WAV = ".wav";

    private static final String TAP_RECORD_DIR = "tap_record";

    @Autowired
    private DUIOkHttpClient duiOkHttpClient;

    @GetMapping("/content/generateWav.json")
    public CommonRes generateWav(HttpServletResponse response, HttpServletRequest request,
                         @RequestParam(value = "text" ,required = true) String text) {
        byte[] tts;
        tts = duiOkHttpClient.ttsJsoup(text, "");
        String sessionId = UUID.randomUUID().toString().replace("-", "");
        String filePath = imgPath + TAP_RECORD_DIR + File.separator + DateUtil.getDateString(new Date()) + File.separator + sessionId + WAV;
        File file = new File(filePath);
        // 如果文件的路径不存在就创建路径
        if (!file.exists()) {
            file.getParentFile().mkdirs();
        }
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            fileOutputStream.write(tts);
            fileOutputStream.flush();
        } catch (IOException e) {
            log.error("写入文件异常，filePath: " + filePath, e);
        }
        return CommonRes.success(filePath);
    }


    @RequestMapping("/material/select.json")
    public CommonRes materialSelect(HttpServletRequest request, @RequestBody MaterialRequest materialReq) {
        if (StringUtils.isBlank(materialReq.getMaterialName()) &&
                StringUtils.isBlank(materialReq.getStage()) &&
                StringUtils.isBlank(materialReq.getStatusInfo())) {
            throw new QgzjException(QgzjStudyExceptionEnum.EMPTY);
        }
        PageQueryRes<TeachingMaterial> materials = materialService.selectMaterial(materialReq);
        return CommonRes.success(materials);
    }

    @RequestMapping("/material/insert.json")
    public CommonRes materialInsert(HttpServletRequest request, @RequestBody MaterialRequest materialReq) {
        if (StringUtils.isBlank(materialReq.getMaterialName()) ||
                StringUtils.isBlank(materialReq.getStage()) ||
                StringUtils.isBlank(materialReq.getCourseIntroduction()) ||
                StringUtils.isBlank(materialReq.getCoverPicUrl())) {
            throw new QgzjException(QgzjStudyExceptionEnum.EMPTY);
        }
        materialService.insertMaterial(materialReq);
        return CommonRes.success();
    }

    @RequestMapping("/material/update.json")
    public CommonRes materialUpdate(HttpServletRequest request, @RequestBody MaterialRequest materialReq) {
        if (StringUtils.isBlank(materialReq.getMaterialNo())) {
            throw new QgzjException(QgzjStudyExceptionEnum.EMPTY_教材编号);
        }
        materialService.updateMaterial(materialReq);
        return CommonRes.success();
    }

    @RequestMapping("/material/delete.json")
    public CommonRes materialDelete(HttpServletRequest request, @RequestBody MaterialRequest materialReq) {
        if (StringUtils.isBlank(materialReq.getMaterialNo())) {
            throw new QgzjException(QgzjStudyExceptionEnum.EMPTY_教材编号);
        }
        materialService.deleteMaterial(materialReq);
        return CommonRes.success();
    }

    @RequestMapping("/volume/select.json")
    public CommonRes volumeSelect(HttpServletRequest request, @RequestBody VolumeRequest volume) {
        if (StringUtils.isBlank(volume.getMaterialNo())) {
            throw new QgzjException(QgzjStudyExceptionEnum.EMPTY_教材编号);
        }
        PageQueryRes<Volume> volumes = volumeService.selectVolume(volume);
        return CommonRes.success(volumes);
    }

    @RequestMapping("/volume/insert.json")
    public CommonRes volumeInsert(HttpServletRequest request, @RequestBody VolumeRequest volume){
        if (StringUtils.isBlank(volume.getMaterialNo())) {
            throw new QgzjException(QgzjStudyExceptionEnum.EMPTY_教材编号);
        }
        if (StringUtils.isBlank(volume.getVolumeName())) {
            throw new QgzjException(QgzjStudyExceptionEnum.EMPTY_册名称);
        }
        if (StringUtils.isBlank(volume.getOrderNo())) {
            throw new QgzjException(QgzjStudyExceptionEnum.EMPTY_册顺序);
        }
        if (null==volume.getPageNum()) {
            throw new QgzjException(QgzjStudyExceptionEnum.EMPTY_册页数);
        }
        volumeService.insertVolume(volume);
        return CommonRes.success();
    }

    @RequestMapping("/volume/update.json")
    public CommonRes volumeUpdate(HttpServletRequest request, @RequestBody VolumeRequest volume){
        if (StringUtils.isBlank(volume.getMaterialNo())) {
            throw new QgzjException(QgzjStudyExceptionEnum.EMPTY_教材编号);
        }
        if (StringUtils.isBlank(volume.getVolumeNo())) {
            throw new QgzjException(QgzjStudyExceptionEnum.EMPTY_册编号);
        }
        volumeService.updateVolume(volume);
        return CommonRes.success();
    }


    @RequestMapping("/volume/delete.json")
    public CommonRes volumeDelete(HttpServletRequest request, @RequestBody VolumeRequest volume) {
        if (StringUtils.isBlank(volume.getMaterialNo())) {
            throw new QgzjException(QgzjStudyExceptionEnum.EMPTY_教材编号);
        }
        if (StringUtils.isBlank(volume.getVolumeNo())) {
            throw new QgzjException(QgzjStudyExceptionEnum.EMPTY_册编号);
        }
        volumeService.deleteVolume(volume);
        return CommonRes.success();
    }

    @RequestMapping("/catalog/select.json")
    public CommonRes catalogSelect(HttpServletRequest request, @RequestBody CatalogRequest catalog) {
        if (StringUtils.isBlank(catalog.getMaterialNo())) {
            throw new QgzjException(QgzjStudyExceptionEnum.EMPTY_教材编号);
        }
        if (StringUtils.isBlank(catalog.getVolumeNo())) {
            throw new QgzjException(QgzjStudyExceptionEnum.EMPTY_册编号);
        }
        PageQueryRes<Catalog> catalogs = catalogService.selectCatalog(catalog);
        return CommonRes.success(catalogs);
    }

    @RequestMapping("/catalog/insert.json")
    public CommonRes catalogInsert(HttpServletRequest request, @RequestBody CatalogRequest catalog){
        if (StringUtils.isBlank(catalog.getMaterialNo())) {
            throw new QgzjException(QgzjStudyExceptionEnum.EMPTY_教材编号);
        }
        if (StringUtils.isBlank(catalog.getVolumeNo())) {
            throw new QgzjException(QgzjStudyExceptionEnum.EMPTY_册编号);
        }
        if(StringUtils.isBlank(catalog.getName())){
            throw new QgzjException(QgzjStudyExceptionEnum.EMPTY_目录名称);
        }
        if(null!=catalog.getStartPage()){
            throw new QgzjException(QgzjStudyExceptionEnum.EMPTY_目录起始页);
        }
        catalogService.insertCatalog(catalog);
        return CommonRes.success();
    }

    @RequestMapping("/catalog/update.json")
    public CommonRes catalogUpdate(HttpServletRequest request, @RequestBody CatalogRequest catalog){
        if (StringUtils.isBlank(catalog.getMaterialNo())) {
            throw new QgzjException(QgzjStudyExceptionEnum.EMPTY_教材编号);
        }
        if (StringUtils.isBlank(catalog.getVolumeNo())) {
            throw new QgzjException(QgzjStudyExceptionEnum.EMPTY_册编号);
        }
        if(StringUtils.isBlank(catalog.getName())){
            throw new QgzjException(QgzjStudyExceptionEnum.EMPTY_目录名称);
        }
        catalogService.updateCatalog(catalog);
        return CommonRes.success();
    }


    @RequestMapping("/catalog/delete.json")
    public CommonRes catalogDelete(HttpServletRequest request, @RequestBody CatalogRequest catalog) {
        if (StringUtils.isBlank(catalog.getMaterialNo())) {
            throw new QgzjException(QgzjStudyExceptionEnum.EMPTY_教材编号);
        }
        if (StringUtils.isBlank(catalog.getVolumeNo())) {
            throw new QgzjException(QgzjStudyExceptionEnum.EMPTY_册编号);
        }
        if(StringUtils.isBlank(catalog.getName())){
            throw new QgzjException(QgzjStudyExceptionEnum.EMPTY_目录名称);
        }
        catalogService.deleteCatalog(catalog);
        return CommonRes.success();
    }

    @RequestMapping("/content/select.json")
    public CommonRes conetntSelect(HttpServletRequest request, @RequestBody ContentRequest content) {
        if (StringUtils.isBlank(content.getMaterialNo())) {
            throw new QgzjException(QgzjStudyExceptionEnum.EMPTY_教材编号);
        }
        if (StringUtils.isBlank(content.getVolumeNo())) {
            throw new QgzjException(QgzjStudyExceptionEnum.EMPTY_册编号);
        }
        if (StringUtils.isBlank(content.getContentNo())) {
            throw new QgzjException(QgzjStudyExceptionEnum.EMPTY_内容编号);
        }
        PageQueryRes<Content> contents = contentService.selectContent(content);
        return CommonRes.success(contents);
    }

    @RequestMapping("/content/insert.json")
    public CommonRes contentInsert(HttpServletRequest request, @RequestBody ContentRequest content){
        if (StringUtils.isBlank(content.getMaterialNo())) {
            throw new QgzjException(QgzjStudyExceptionEnum.EMPTY_教材编号);
        }
        if (StringUtils.isBlank(content.getVolumeNo())) {
            throw new QgzjException(QgzjStudyExceptionEnum.EMPTY_册编号);
        }
        if(null!=content.getCurrentPage()){
            throw new QgzjException(QgzjStudyExceptionEnum.EMPTY_内容当前页);
        }
        contentService.insertContent(content);
        return CommonRes.success();
    }

    @RequestMapping("/content/update.json")
    public CommonRes contentUpdate(HttpServletRequest request, @RequestBody ContentRequest content){
        if (StringUtils.isBlank(content.getMaterialNo())) {
            throw new QgzjException(QgzjStudyExceptionEnum.EMPTY_教材编号);
        }
        if (StringUtils.isBlank(content.getVolumeNo())) {
            throw new QgzjException(QgzjStudyExceptionEnum.EMPTY_册编号);
        }
        if(StringUtils.isBlank(content.getContentNo())){
            throw new QgzjException(QgzjStudyExceptionEnum.EMPTY_内容编号);
        }
        contentService.updateContent(content);
        return CommonRes.success();
    }


    @RequestMapping("/content/delete.json")
    public CommonRes contentDelete(HttpServletRequest request, @RequestBody ContentRequest content) {
        if (StringUtils.isBlank(content.getMaterialNo())) {
            throw new QgzjException(QgzjStudyExceptionEnum.EMPTY_教材编号);
        }
        if (StringUtils.isBlank(content.getVolumeNo())) {
            throw new QgzjException(QgzjStudyExceptionEnum.EMPTY_册编号);
        }
        if(StringUtils.isBlank(content.getContentNo())){
            throw new QgzjException(QgzjStudyExceptionEnum.EMPTY_内容编号);
        }
        contentService.deleteContent(content);
        return CommonRes.success();
    }

    @RequestMapping("/contentDetail/select.json")
    public CommonRes contentDetailSelect(HttpServletRequest request, @RequestBody ContentDetailRequest contentDetailReq) {
        if (StringUtils.isBlank(contentDetailReq.getMaterialNo())) {
            throw new QgzjException(QgzjStudyExceptionEnum.EMPTY_教材编号);
        }
        if (StringUtils.isBlank(contentDetailReq.getVolumeNo())) {
            throw new QgzjException(QgzjStudyExceptionEnum.EMPTY_册编号);
        }
        if (StringUtils.isBlank(contentDetailReq.getContentNo())) {
            throw new QgzjException(QgzjStudyExceptionEnum.EMPTY_内容编号);
        }
        PageQueryRes<ContentDetail> contentDetails = contentDetailService.selectContentDetail(contentDetailReq);
        return CommonRes.success(contentDetails);
    }

    @RequestMapping("/contentDetail/insert.json")
    public CommonRes contentDetailInsert(HttpServletRequest request, @RequestBody ContentDetailRequest contentDetailReq){
        if (StringUtils.isBlank(contentDetailReq.getMaterialNo())) {
            throw new QgzjException(QgzjStudyExceptionEnum.EMPTY_教材编号);
        }
        if (StringUtils.isBlank(contentDetailReq.getVolumeNo())) {
            throw new QgzjException(QgzjStudyExceptionEnum.EMPTY_册编号);
        }
        if (StringUtils.isBlank(contentDetailReq.getContentNo())) {
            throw new QgzjException(QgzjStudyExceptionEnum.EMPTY_内容编号);
        }
        if (StringUtils.isBlank(contentDetailReq.getContentText())) {
            throw new QgzjException(QgzjStudyExceptionEnum.EMPTY_内容详情文本);
        }
        if (StringUtils.isBlank(contentDetailReq.getVoiceAddress())) {
            throw new QgzjException(QgzjStudyExceptionEnum.EMPTY_内容详情录音地址);
        }
        contentDetailService.insertContentDetail(contentDetailReq);
        return CommonRes.success();
    }

    @RequestMapping("/contentDetail/update.json")
    public CommonRes contentDetailUpdate(HttpServletRequest request, @RequestBody ContentDetailRequest contentDetailReq){
        if (StringUtils.isBlank(contentDetailReq.getMaterialNo())) {
            throw new QgzjException(QgzjStudyExceptionEnum.EMPTY_教材编号);
        }
        if (StringUtils.isBlank(contentDetailReq.getVolumeNo())) {
            throw new QgzjException(QgzjStudyExceptionEnum.EMPTY_册编号);
        }
        if(StringUtils.isBlank(contentDetailReq.getContentNo())){
            throw new QgzjException(QgzjStudyExceptionEnum.EMPTY_内容编号);
        }
        contentDetailService.updateContentDetail(contentDetailReq);
        return CommonRes.success();
    }


    @RequestMapping("/contentDetail/delete.json")
    public CommonRes contentDetailDelete(HttpServletRequest request, @RequestBody ContentDetailRequest contentDetailReq) {
        if (StringUtils.isBlank(contentDetailReq.getMaterialNo())) {
            throw new QgzjException(QgzjStudyExceptionEnum.EMPTY_教材编号);
        }
        if (StringUtils.isBlank(contentDetailReq.getVolumeNo())) {
            throw new QgzjException(QgzjStudyExceptionEnum.EMPTY_册编号);
        }
        if(StringUtils.isBlank(contentDetailReq.getContentNo())){
            throw new QgzjException(QgzjStudyExceptionEnum.EMPTY_内容编号);
        }
        contentDetailService.deleteContentDetail(contentDetailReq);
        return CommonRes.success();
    }


    @GetMapping("/content/confirmText.json")
    public CommonRes add(HttpServletResponse response, HttpServletRequest request,
                         @RequestBody ContentRequest contentRequest) {
        if (contentRequest.getId() == null) {
            throw new QgzjException(QgzjStudyExceptionEnum.EMPTY_内容id);
        }
        Content content = contentMapper.selectById(contentRequest.getId());
        if (content == null) {
            throw new QgzjException(QgzjStudyExceptionEnum.NOT_EXIST_内容);
        }

        contentDetailService.deleteByContentNo(contentRequest.getContentNo());
        List<ContentDetailRequest> detailRequests = contentRequest.getDetailRequests();
        for (ContentDetailRequest detailRequest : detailRequests) {
            contentDetailService.insertContentDetail(detailRequest);
        }

        return CommonRes.success();
    }



}
